import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;


//        1.读取文件
//        2.获得名字
//        3.随机产生不重复6位同学
//        4.打成jar 包
//        5.做成批处理程序  双击可以运行

public class 点名器 implements java.io.Serializable {
//使用 BufferedReader 在控制台读取字符

    //        用于接收文件输入流的字符串数组
    public ArrayList<String> arrayList = new ArrayList<String>();
    //获得过的随机数组
    public ArrayList<Integer> receivedRandomList = new ArrayList<>();
    public Iterator<Integer> iterator;
    public int iteratorNumber;


    public static void main(String[] args) {
        点名器 dianm = null;
        try {
            dianm = new 点名器();
        } catch (IOException e) {
            e.printStackTrace();
        }


        String order = new String();
        int number = 0;
        Scanner scan = new Scanner(System.in);


        if (args.length > 0) {
            order = args[0];
            if (args.length > 1) {
                number = Integer.valueOf(args[1]);
            }
            dianm.doOrder(order,number);
        } else {
            order = "";


            while (true) {
                dianm.doOrder(order, number);


                System.out.print("操作：");
                // 判断是否还有输入
                if (scan.hasNextLine()) {
                    order = scan.nextLine();
                }
                String[] arrayListOrder = order.split(" ");
                if (arrayListOrder.length > 0) {
                    order = arrayListOrder[0];
                    if (arrayListOrder.length > 1) {
                        number = Integer.valueOf(arrayListOrder[1]);
                    } else number = 0;
                }
            }
        }
    }


    public void doOrder(String order, int number) {
        switch (order) {
            case "-p":
            case "--have":
            case "--print":
                if (number > 0) {
                    this.printTime(number);
                } else this.printTime();
                break;
            case "-r":
            case "--reset":
                this.reset();
                System.out.println("已重置选中列表。。。");
                break;
            case "-a":
            case "--all":
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i % 3 == 0) System.out.println();
                    System.out.print(arrayList.get(i) + "  \t");
                }
                System.out.println();
                break;
            case "-h":
            case "--help":
                System.out.println("\n\n\n\n" +
                        "\t\t\t  帮助：\n" +
                        "\t\t\t* -p \t--have \t--print\t\t输出一条数据。\n" +
                        "\t\t\t* -r \t--reset\t\t\t清除所有抽过的同学。\n" +
                        "\t\t\t* -a \t--all\t\t\t输出所有的同学信息\n" +
                        "\t\t\t* -h \t--help\t\t\t查看帮助\n");
                break;
            case "-e":
            case "--exit":
                System.exit(0);
            default:
                System.out.println("你可能想输入-h 或 --help");
        }
    }

    点名器() throws IOException {
        this.InputToArrayList();
        try {
            this.receivedRandomList = (ArrayList<Integer>) this.loadObject("receivedRandomList.ser");
            this.initIterator();//初始化为0
        } catch (FileNotFoundException c) {
            this.RandomNumberGroup();
            this.savaObject(this.receivedRandomList, "receivedRandomList.ser");
            this.receivedRandomList = (ArrayList<Integer>) this.loadObject("receivedRandomList.ser");

            this.initIterator();//初始化为0
            iteratorNumber = 0;
            this.savaObject(iteratorNumber, "iteratorNumber.ser");
        } catch (IOException c) {
            c.printStackTrace();
        }

        try {
            this.iteratorNumber = (int) this.loadObject("iteratorNumber.ser");
            for (int i = 0; i < iteratorNumber; i++) {
                iterator.next();
            }
        } catch (FileNotFoundException c) {
            iteratorNumber = 0;
            this.savaObject(iteratorNumber, "iteratorNumber.ser");
        } catch (IOException c) {
            c.printStackTrace();
        }

    }

    public void reset() {
        this.RandomNumberGroup();
        this.initIterator();
        this.iteratorNumber = 0;
        this.savaObject(iteratorNumber, "iteratorNumber.ser");
        this.savaObject(this.receivedRandomList, "receivedRandomList.ser");
    }

    public void printTime() {
        printTime(1);
    }

    public void printTime(int time) {
        for (int i = 0; i < time; i++) {
            if (this.hasNext()) {
                this.next();
            } else {
                System.out.println("所有的同学都已抽过。。。。。命令：-r可重置点名名单");
                break;
            }
        }
        this.savaObject(this.iteratorNumber, "iteratorNumber.ser");
    }


    private boolean hasNext() {
        if (iterator.hasNext()) {
            return true;
        } else if (iteratorNumber == arrayList.size()) {
            return false;
        } else if (iteratorNumber > arrayList.size()) {
            return false;
        } else return false;
    }

    private void next() {
        if (iterator.hasNext()) {
//            System.out.println(iterator.next());
            System.out.println(arrayList.get(iterator.next()));
            this.iteratorNumber++;
        } else if (iteratorNumber == arrayList.size()) {
            System.out.println("今天所有的同学都已抽过。。。");
            System.out.println("建议您重置点名。。。");
        }
    }

    private void savaObject(Object obj, String strPath) {
        try {
            FileOutputStream fileOut =
                    new FileOutputStream(strPath);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(obj);
            out.close();
            fileOut.close();
        } catch (IOException i) {
            i.printStackTrace();
        }
    }

    private Object loadObject(String strPath) throws IOException {
        Object obj = null;
        try {
            FileInputStream fileIn = new FileInputStream(strPath);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            obj = in.readObject();
            in.close();
            fileIn.close();
        } catch (ClassNotFoundException c) {
            System.out.println("class not found");
            c.printStackTrace();
            return obj;
        }
        return obj;
    }

    private void initIterator() {
        this.iterator = this.receivedRandomList.iterator();
    }

    private void OutputToFile() throws IOException {
//        文件输出
        {
            File f = new File("学生目录.csv");
            FileOutputStream fop = new FileOutputStream(f);
            // 构建FileOutputStream对象,文件不存在会自动新建

            OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
            // 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk

            writer.append("序号,姓名,电话");
            // 写入到缓冲区

            writer.append("\r\n");
            // 换行


            writer.close();
            // 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉

            fop.close();
            // 关闭输出流,释放系统资源
        }
    }

    private void InputToArrayList() throws IOException {

        //用于接收文件输入流的字符串
        String strInput = new String();


        //文件读取
        {
            File f = new File("学生目录.csv");
            FileInputStream fip = new FileInputStream(f);
            // 构建FileInputStream对象

//            InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
            InputStreamReader reader = new InputStreamReader(fip, "GBK");

            // 构建InputStreamReader对象,编码与写入相同

            StringBuffer sb = new StringBuffer();
            while (reader.ready()) {
                sb.append((char) reader.read());
                // 转成char加到StringBuffer对象中
            }
            strInput = sb.toString();
            //传给用于接收文件输入流的字符串
            reader.close();
            // 关闭读取流

            fip.close();
            // 关闭输入流,释放系统资源
        }

        //文件读取后的处理
        {
            strInput = strInput.split("\n", 2)[1];
            for (String item : strInput.split("\\r\\n")) {
                arrayList.add(item);
            }
        }

    }


    /**
     * 重新填充receivedRandomList随机数组
     */
    private void RandomNumberGroup() {
        receivedRandomList = new ArrayList<Integer>();
        //随机
        {
            Random random = new Random();

            for (int i = 0; i < arrayList.size(); i++) {

                int item = random.nextInt(arrayList.size());
                //存在一样就换随机数
                Exist ex = new Exist(receivedRandomList.iterator());
                while (ex.existOfInt(item)) {
                    item = random.nextInt(arrayList.size());
                    ex.setIterator(receivedRandomList.iterator());
                }

                receivedRandomList.add(item);
            }
        }
    }

    /**
     * 判断是否存在
     *
     * @description:
     * @Author 陈Ser
     * @Date 2021/9/4
     * @Version 1.0
     */
    private class Exist {
        private Iterator iterator;

        Exist(Iterator iterator) {
            this.iterator = iterator;
        }


        /**
         * 使用iterator.next() == i)判断
         *
         * @param i 用于判断的数据
         * @return true为存在
         * @description: 使用iterator.next() == i)判断
         * @Author 陈Ser
         * @Date 2021/9/4
         * @Version 1.0
         */
        public <E> boolean existOfInt(E i) {
            while (iterator.hasNext()) {
                E item = (E) iterator.next();
                if (item == i) return true;

//                if (iterator.next() == i) return true;
            }
            return false;
        }

        public void setIterator(Iterator iterator) {
            this.iterator = iterator;
        }
    }
}
